
import React from "react";
import createYGCPage from '../../../components/CreateYGCPage/index.jsx'
import YGCupload from '../../../components/YGCCreateUpload';
import 'bee-table/build/Table.css';

import getFormItems from './../tpl/formTpl.js'
import { btnCardTpl } from './../tpl/btnCardTpl.js'

import afterEvent from './../card/afterEvent.js';
import buttonClick from './../card/buttonEvent.js';
import billIcon from './../../../assets/icon/ygc_resume_work.png';

import { saveResumeWorkBill, initPage, ConstValues } from '../projectIndicators.config.js';


class ResumeWorkCard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            result: '',
            pageStatus: ''
        };
        this.srcHeight = 60;
    }

    componentDidMount() {
        let result = JSON.parse(window.sessionStorage.getItem('pageInfo'));
        let pageStatus = window.sessionStorage.getItem('pageStatus');
        this.setState({ result, pageStatus });
    }

    componentDidUpdate = (prevProps, prevState) => {
        if(prevState.pageStatus != this.state.pageStatus) {
            initPage.call(this, {result: this.state.result, pageStatus: this.state.pageStatus});
        }
        // 处理按钮显隐
        if(JSON.stringify(prevState.result) != JSON.stringify(this.state.result)) {
            if(this.state.result) {
                let { billstatus } = this.state.result;
                if(billstatus === 7 || billstatus === 5) {
                    this.props.button.setButtonDisable('approve', false);
                } else {
                    this.props.button.setButtonDisable('approve', true);
                }
            }
        }
    };
    
    componentWillUnmount = () => {
        window.sessionStorage.clear();
    };

    
    // 指派人选择
    handlerConfirmChooseApporea = (data) => {
        saveResumeWorkBill(data).then((res) => {
            let { resultCode, result, exMsg } = res.data;
            if(resultCode === 'ok') {
                this.setState({ result, pageStatus: 'browse'}, () => {
                    initPage.call(this, {result: this.state.result, pageStatus: this.state.pageStatus});
                });
            } else {
                this.props.approve.closeApproveModal();
                this.props.vendorsDanger({text: exMsg});
            }
        })
    }

    // 审批、驳回
    handlePassReject = (data) => {
        let { record } = data;
        this.setState({result: record}, () => {
            initPage.call(this, {result: this.state.result, pageStatus: this.state.pageStatus});
            if(this.state.result) {
                let { billstatus } = this.state.result;
                if(billstatus !== 7) {
                    this.props.button.setButtonDisable('approve', true);
                } 
            }
        });
        this.props.passreject.closePassRejectModal();
    }

    render() {
        return (
            <div className="ygc-card">
                <div className='ygc-card-header'>
                    {this.props.YGCreateCardTitle({
                        title: '复工单',
                        billIcon,
                        backRoute: ()=>{
                            if(window.sessionStorage.getItem('resumeWorkListData')){
                                window.sessionStorage.setItem('resumeworkFromCardBrowse','true');
                            }
                            window.location.href = `./resumeworklist.html`;
                        },
                        pageStatus: this.state.pageStatus
                    })}
                    <div className='ygc-card-header-button'>
                        {this.props.YGCreateButton({
                            btnTpl: btnCardTpl,
                            buttonClick: buttonClick.bind(this)
                        })}
                    </div>
                </div>
                <div className="ygc-card-body">
                    <div className="ygc-card-body-form">
                        {this.props.YGCCreateForm({ items: getFormItems.call(this), afterEvent: afterEvent.bind(this) })}
                    </div>
                    <div className="ygc-card-body-table"></div>
                </div>
                {this.props.YGCreateApproveModal({ handlerConfirm: this.handlerConfirmChooseApporea })}
                {this.props.YGCreatePassRejectModal({ handlePassReject: this.handlePassReject })}
                {this.props.YGCreateAproHistoryModal({})}
                {this.state.result && <YGCupload
					ref={(upload) => { this.ygcUpload = upload }}
					billType={'TGBG'}
					pkBill={this.state.result[ConstValues.Primarykey]}
					pkProject={this.state.result['pkProject']}
				/>}
            </div>

        )
    }
}

export default createYGCPage({})(ResumeWorkCard);
